package com.yyk.yiliao.ui.trade;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;

import com.orhanobut.hawk.Hawk;
import com.orhanobut.logger.Logger;
import com.yyk.yiliao.R;
import com.yyk.yiliao.base.BaseActivity;
import com.yyk.yiliao.ui.trade.widget.PasswordInputView;
import com.yyk.yiliao.util.Md5Util2;
import com.yyk.yiliao.util.rx.ApiService;
import com.yyk.yiliao.util.rx.RxUtils;
import com.yyk.yiliao.bean.UserbankSetpwdInfo;

import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;
import java.util.TreeMap;

import butterknife.BindView;
import butterknife.OnClick;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * 设置交易密码    --填写第二次的交易密码
 */
public class InvestPassword3Activity extends BaseActivity {
    public static final int RESULT_OK = 2; //大于1

    @BindView(R.id.bt_next)
    Button btNext;
    @BindView(R.id.pwd_inputview)
    PasswordInputView pwdInputview;


    private String password;
    private String tel;
    private String code;

    @Override
    public int getLayoutId() {
        return R.layout.activity_invest_password3;
    }

    @Override
    public void initView() {
        initToolbar();
        initTextListenter();
    }

    private void initToolbar() {
        setBackArrow();
        setTitle("设置交易密码");
    }

    @Override
    public void initData() {
        initGetIntent();

    }


    @Override
    protected void onResume() {
        super.onResume();
        initRuanjianpan();
    }

    /**
     * 自动弹出键盘输入框
     */
    private void initRuanjianpan() {
        pwdInputview.setFocusable(true);
        pwdInputview.setFocusableInTouchMode(true);
        pwdInputview.requestFocus();
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
                           public void run() {
                               InputMethodManager inputManager =
                                       (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                               inputManager.showSoftInput(pwdInputview, 0);
                           }
                       },
                200);
    }


    private void initTextListenter() {
        pwdInputview.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s.length() > 5) {
                    String string = pwdInputview.getText().toString();
                    //你的锅，两个tosat互相顶掉了
//                    ToastUtil.showShort(mActivity, string);
                    if (password.equals(string) || password == string) {
//                        Intent intent = new Intent();
//                        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
//                        startAct(intent, SafeSetActivity.class, null);

                        Map<String, String> map = new TreeMap<>();
                        map.put("id", Hawk.get("uid") + "");
                        map.put("tel", tel + "");
                        map.put("code", code + "");
                        map.put("pwd", password + "");
                        map.put("sign", Md5Util2.createSign(map));
                        RxUtils.getInstance().retrofit.create(ApiService.class).postUserbankSetpwd(map)
                                .subscribeOn(Schedulers.io())
                                .observeOn(AndroidSchedulers.mainThread())
                                .unsubscribeOn(Schedulers.io())
                                .subscribe(new Subscriber<UserbankSetpwdInfo>() {
                                    @Override
                                    public void onCompleted() {

                                    }

                                    @Override
                                    public void onError(Throwable e) {

                                    }

                                    @Override
                                    public void onNext(UserbankSetpwdInfo info) {
                                        Logger.e("设置支付密码" + info.toString());
                                        if (info.getCode() == 1) {
                                            if (InvestPasswordActivity.intance != null) {
                                                InvestPasswordActivity.intance.finish();
                                            }
                                            if (InvestPassword2Activity.intance != null) {
                                                InvestPassword2Activity.intance.finish();
                                            }
                                            finish();
                                        }
                                    }
                                });


                    } else {
                        // 设置返回数据
                        Bundle bundle = new Bundle();
                        bundle.putString("", "密码不一致");
                        Intent intent = new Intent();
                        intent.putExtras(bundle);
                        //1.代表 清空 密码
                        setResult(RESULT_OK, intent);
                        finish();

                    }


                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

    }

    private void initGetIntent() {
        if (getBundle() != null) {
            password = getBundle().getString("pwd");
            tel = getBundle().getString("tel");
            code = getBundle().getString("code");
        }

    }

    @OnClick(R.id.bt_next)
    public void onViewClicked() {
        startAct(RechargeActivity.class);
    }


}
